<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()"
    >

      <el-form-item
        label="考核内容"
        prop="assessmentContent"
        style="margin-bottom:10px;"
      >
        <el-input
          v-model="dataForm.assessmentContent"
          placeholder="考核内容" disabled
        />
      </el-form-item>
      <el-form-item
        label="类别"
        prop="category"
        style="margin-bottom:10px;"
      >
        <el-input
          v-model="dataForm.category"
          placeholder="类别"
        />
      </el-form-item>
      <el-form-item
        label="标准"
        prop="standard"
        style="margin-bottom:10px;"
      >
        <el-input type="textarea"
                  v-model="dataForm.standard"
                  placeholder="标准" disabled/>
      </el-form-item>

      <el-form-item label="状态：">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>


    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          assessmentContent: '',
          category: '',
          standard: null,
          deductionRange: '',
          status: ''
        },
        dataRule: {
          assessmentContent: [
            { required: true, message: '考核内容不能为空', trigger: 'blur' }
          ],
          // category: [
          //   { required: true, message: '类别不能为空', trigger: 'blur' }
          // ],
          standard: [
            { required: true, message: '标准不能为空', trigger: 'blur' }
          ],
          deductionRange: [
            { required: true, message: '扣分限制不能为空', trigger: 'blur' }
          ]
        },
        options: [{
          value: 0,
          label: '未启用'
        }, {
          value: 1,
          label: '启用'
        }
        ]
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick (() => {
          this.$refs['dataForm'].resetFields ()
          if (this.dataForm.id) {
            axios ({
              url: `/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/queryRuleById/${this.dataForm.id}`,
              method: 'get'
            }).then (({ data }) => {
              if (data && data.code === '00000') {
                var data = AesDecrypt (data.result)
                this.dataForm.assessmentContent = data.result.assessmentContent
                this.dataForm.category = data.result.category
                this.dataForm.standard = data.result.standard
                this.dataForm.deductionRange = data.result.deductionRange
                this.dataForm.status = data.result.status
              }
            }).catch(error => {
          this.$message.error ('服务器访问异常，请从新登录')
        });
          }
        })
      },

      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate ((valid) => {
          if (valid) {
            var uu = {
              'id': this.dataForm.id || undefined,
              'assessmentContent': this.dataForm.assessmentContent,
              'category': this.dataForm.category,
              'standard': this.dataForm.standard,
              'deductionRange': this.dataForm.deductionRange,
              'status': this.dataForm.status
            }
            var jsonString = JSON.stringify (uu)
            var yy = Encrypt (jsonString)
            var encodedString = encodeURIComponent (yy)
            axios ({
              url: `/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/${!this.dataForm.id ? 'create' : 'update'}`,
              method: 'post',
              data: encodedString
            }).then (({ data }) => {
              if (data && data.code === '00000') {
                this.$message ({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit ('refreshDataList')
                  }
                })
              } else {
                this.$message.error (data.msg)
              }
            }).catch(error => {
          this.$message.error ('服务器访问异常，请从新登录')
        });
          }
        })
      }
    }
  }
</script>
